var app = getApp();
Page({
  data:{
    page : {},
    bar : {},
    copy : null,
    pageid : 0,
    article : null,
  },
    onLoad:function(options){
        var that = this;

        app.com.http('getpage','GET',options,10,false,function(res){
            console.log(res);
            var page = res.data.data.page;
            var bar = res.data.data.bar;
            // 跳转web 拦截
            if( bar.data[0].type == 'weburl' && bar.data[0].weburl ){
                var weburl = encodeURIComponent( bar.data[0].weburl );
                wx.redirectTo({
                  url: '/zofui_sitetemp/pages/webview/webview?url='+weburl,
                });
                return false;
            }

            if (!res.data.errno) {
                that.setData({
                    page : page.params,
                    pageid : page.id
                });
                for (var i = 0; i < that.data.page.data.length; i++) {
                    if( i == 0 ) {
                        var WxParse = require('../../resource/wxParse/wxParse.js');
                    }
                    if( that.data.page.data[i].name == 'text' ) {
                        var dataname = 'page.data['+i+'].params.node';
                        WxParse.wxParse( dataname, 'html',  that.data.page.data[i].params.content, that, 0);
                    }
                    if( that.data.page.data[i].name == 'video' && that.data.page.data[i].params.type == 0 ){
                        (function(m){
                            app.com.http('gettenvedio','POST',{url:that.data.page.data[m].params.url},0,false,function(resa){
                              //console.log(resa);
                                if (!res.data.errno) {
                                    
                                    var dataset = 'page.data['+m+'].params.upurl';

                                    that.setData({
                                        [dataset] : resa.data.message
                                    });

                                }
                            })
                        })(i)
                    }
                }
                
                if( page.params.basic.isbar == 0 ){
                    that.setData({
                        bar : bar,
                    });
                }
                if( res.data.data.article ){
                  //console.log(res.data.data.article);
                    that.setData({
                        article : res.data.data.article,
                    });
                }
                
                if( res.data.data.copy ){
                    var WxParse = require('../../resource/wxParse/wxParse.js');
                    WxParse.wxParse( 'copy', 'html',  res.data.data.copy, that, 0);
                }
                
                wx.setNavigationBarTitle({
                  title: page.params.basic.title,
                });

                // 顶部颜色
                if( page.params.basic.topcolor && page.params.basic.topbg ) {
                    wx.setNavigationBarColor({
                        frontColor: page.params.basic.topcolor,
                        backgroundColor: page.params.basic.topbg,
                    });
                }

            }
        });       

    },
    onReady:function(){
        // 页面渲染完成
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.page.basic.sharetitle,
            path: '',
            imageUrl : self.data.page.basic.shareimg,
        }
    },
    imageLoad : function(e){
        var self = this;
        //获取图片真实宽度  
        var imgwidth = e.detail.width,
        imgheight = e.detail.height;
        //宽高比
        var ratio = 1; 
        wx.getSystemInfo({ 
            success: function (res) {
                ratio = res.windowWidth / imgwidth;
            }
        });

        var viewHeight = imgheight*ratio;
        for (var i = 0; i < self.data.page.data.length; i++) {
            if( self.data.page.data[i].id == e.target.dataset.no ) {
                for (var j = 0; i < self.data.page.data[i].params.data.length; j++) {
                    var thisdata = self.data.page.data[i].params.data[j];   
                    if( thisdata.id == e.target.dataset.inno ) {

                        var setda = 'page.data['+i+'].params.data['+j+'].height';
                        
                        self.setData({
                            [setda]: viewHeight,
                        });
                        if( j == 0 ){ // 设置初始的current
                            var setda2 = 'page.data['+i+'].params.current';
                            self.setData({
                                [setda2]: 0,
                            }); 
                        }
                        return false;
                    }
                }
                return false;
            }
        }
    },
    bindDateChange : function(e){
        var self = this;
        var data = self.data.page.data;
        for (var i = 0; i < data.length; i++) {
            if( data[i].id == e.target.dataset.i ) {
                
                for (var j = 0; j < data[i].params.data.length; j++) {
                    if( data[i].params.data[j].id == e.target.dataset.n ) {
                        var setda = 'page.data['+i+'].params.data['+j+'].time';
                        self.setData({
                            [setda]: e.detail.value,
                        });
                        return false;
                    }
                }
            }
        }        
    },
    bindTimeChange : function(e){
        var self = this;
        var data = self.data.page.data;
        for (var i = 0; i < data.length; i++) {
            if( data[i].id == e.target.dataset.i ) {
                
                for (var j = 0; j < data[i].params.data.length; j++) {
                    if( data[i].params.data[j].id == e.target.dataset.n ) {
                        var setda = 'page.data['+i+'].params.data['+j+'].time';
                        self.setData({
                            [setda]: e.detail.value,
                        });
                        return false;
                    }
                }
            }
        }  
    },
    bindRegionChange : function(e){
        var self = this;
        var data = self.data.page.data;
        for (var i = 0; i < data.length; i++) {
            if( data[i].id == e.target.dataset.i ) {
                
                for (var j = 0; j < data[i].params.data.length; j++) {
                    if( data[i].params.data[j].id == e.target.dataset.n ) {
                        var setda = 'page.data['+i+'].params.data['+j+'].region';
                        self.setData({
                            [setda]: e.detail.value,
                        });
                        return false;
                    }
                }
            }
        }        
    },
   
    // 上传图片
    imageto1: function (e) {
        var a = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                a.setData({
                    image1: res.tempFilePaths,
                    show1:"display:none"
                });

//console.log(tempFilePaths);
                wx.uploadFile({
                  url: 'https://xianse.ziyuinfor.com/app/index.php?i=5&t=0&v=1.2.1&from=wxapp&c=entry&a=wxapp&do=uploadform&m=zofui_sitetemp',
                    filePath: a.data.image1[0],
                    name: 'file',
                    formData: {
                    },
                    success: function (ds) {
                        console.log(ds);
                        a.data.images = ds.data;
                        a.setData({
                            images: a.data.images
                        });
                      
                    }
                })
            }
        })
    },
   
    formSubmit : function(e){
        var self = this;
        var id =e.detail.value['zofui_sitetemp_fid'];
        var index = 0;
        for ( var t in e.detail.value ) {
            for (var i = 0; i < self.data.page.data.length; i++) {
                var indata = self.data.page.data[i];
                if( indata.id == id ){
                    var list = indata.params.data;
                    console.log(list);
                    for (var j = 0; j < list.length; j++) {
                        
                        if( j == index && ( list[j].ismust == 0 || typeof list[j].ismust == 'undefined' ) ){
                            if( typeof e.detail.value[t] == 'string' && e.detail.value[t] == '' ){
                                app.util.message('请填写'+t,'','error');
                                return false;
                            }else if( typeof e.detail.value[t] == 'object' && e.detail.value[t].length <= 0 ) {
                                app.util.message('请选择'+t,'','error');
                                return false;
                            }
                            
                        }
                    }
                    
                }
            }
            index ++;
        }
        
        delete e.detail.value['zofui_sitetemp_fid'];
       
        var data = JSON.stringify( e.detail.value );
        // console.log(data);
        app.com.http('saveform','POST',{data:data},0,true,function(res){
            if (!res.data.errno) {
                app.util.message('已提交','redirect:/zofui_sitetemp/pages/page/page?pid='+self.data.pageid,'error');
            }
        });
    },
    
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    }

})